package com.example.databinding_demo.livedata;

import android.view.View;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.Random;

public class UserViewModel extends ViewModel {
    private MutableLiveData<String> mName;

    public MutableLiveData<String> getName() {
        if(mName == null){
            mName = new MutableLiveData<>();
        }
        mName.setValue("张 三 风");
        return mName;
    }

    public void updateData(View view){
        String anotherName = "John Doe";
        Random random = new Random();
        mName.setValue(random.nextInt(1000)+" "+random.nextInt(1000));
    }

    @BindingAdapter({"changeText"})
    public static void changeText(TextView view, String str){
        view.setText(str.replace(" ", "="));
    }
}

